#include "stdafx.h"
#include "CommonDef.h"
#include "ChessPieceView.h"
#include "Player.h"
#include "ChessPiece.h"

HRESULT ChessPieceView::Initialize(std::shared_ptr<Player>& player1, std::shared_ptr<Player>& player2)
{
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_NULL(m_player1 = player1)
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_NULL(m_player2 = player2)

    return S_OK;
}

HRESULT ChessPieceView::DrawChessPieces(const HWND& hwnd)
{
    // Draw player1 chess pieces
    std::vector<ChessPiece*> vec_player1_chess_pieces;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(m_player1.get()->GetAllPieces(vec_player1_chess_pieces))
    size_t player1_chess_piece_num = vec_player1_chess_pieces.size();
    for (int index = 0; index < player1_chess_piece_num; ++index)
    {
        CHESS_TYPE chess_type = e_error_type;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(vec_player1_chess_pieces[index]->GetChessType(chess_type))

        RECT rect_pos;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(vec_player1_chess_pieces[index]->GetRectPos(rect_pos))

        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawChessPiecesFromChessType(hwnd, chess_type, rect_pos, 1))
    }

    // Draw player2 chess pieces
    std::vector<ChessPiece*> vec_player2_chess_pieces;
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(m_player2.get()->GetAllPieces(vec_player2_chess_pieces))
    size_t player2_chess_piece_num = vec_player2_chess_pieces.size();
    for (int index = 0; index < player2_chess_piece_num; ++index)
    {
        CHESS_TYPE chess_type = e_error_type;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(vec_player2_chess_pieces[index]->GetChessType(chess_type))

        RECT rect_pos;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(vec_player2_chess_pieces[index]->GetRectPos(rect_pos))

        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawChessPiecesFromChessType(hwnd, chess_type, rect_pos, 2))
    }

    return S_OK;
}

HRESULT ChessPieceView::DrawChessPiecesFromChessType(
    const HWND& hwnd, const CHESS_TYPE& chess_type, const RECT& rect, int player_index)
{
    HBITMAP image;
    char app_path[MAX_PATH + 1];
    ::GetModuleFileName(NULL, app_path, MAX_PATH);
    (_tcsrchr(app_path, _T('\\')))[1] = 0;
    ::PathRemoveBackslash(app_path);
    (_tcsrchr(app_path, _T('\\')))[1] = 0;
    ::PathRemoveBackslash(app_path);
    //(_tcsrchr(app_path, _T('\\')))[1] = 0;
    //::PathRemoveBackslash(app_path);
    //(_tcsrchr(app_path, _T('\\')))[1] = 0;
    //::PathRemoveBackslash(app_path);

    std::string image_path = "";
    image_path.append(app_path);
    image_path.append("\\res\\");

    switch (chess_type)
    {
    case e_error_type:
        ErrorLogger::WriteLog(__FUNCTION__, "chess type is error.");
        break;
    case e_king:
        image_path.append("King");
        break;
    case e_queue:
        image_path.append("Queue");
        break;
    case e_bishop:
        image_path.append("Bishop");
        break;
    case e_knight:
        image_path.append("Knight");
        break;
    case e_rook:
        image_path.append("Rook");
        break;
    case e_pawn:
        image_path.append("Pawn");
        break;
    default:
        break;
    }

    if (player_index == 1)
    {
        image_path.append("_Write.bmp");
    }
    else
    {
        image_path.append("_Black.bmp");
    }

    image = (HBITMAP)LoadImage(NULL, image_path.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    HDC hdc = ::GetDC(hwnd);
    HDC mem_dc = ::CreateCompatibleDC(hdc);
    HBITMAP old_image = (HBITMAP)::SelectObject(mem_dc, image);
    ::BitBlt(hdc, rect.left, rect.top, rect.right, rect.bottom, mem_dc, 0, 0, SRCCOPY);
    ::SelectObject(mem_dc, old_image);
    ::DeleteObject(image);
    ::DeleteObject(mem_dc);
    ::ReleaseDC(hwnd, hdc);

    return S_OK;
}